package BandB.Tool.GPhoneManager.Widget.Wifi;

import BandB.Tool.GPhoneManager.R;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.RemoteViews;

public class WifiOnOff2Widget extends AppWidgetProvider {

	private final String TAG = "WifiOnOff2Widget";

	// Kingson
	// private static WifiManager wifiManager;
	// private static AppWidgetManager mAppWidgetManager;
	// Better:AppWidgetManager appWidgetManager =
	// AppWidgetManager.getInstance(context);
	// Not use static variable ,because will lost point,will be null ,so user
	// the Better sentence
	// to get the instance everywhere

	private boolean isWifiConnected;
	// private SupplicantState supplicantState;
	private int currentWifiState;
	// private static int[] mAppWidgetIds;

	// wifi status icon
	private final int RES_ID_TURN_ON = R.drawable.wifi3;
	private final int RES_ID_TURN_OFF = R.drawable.wifi;
	private final int RES_ID_TURNING_ON = R.drawable.wifi1;
	private final int RES_ID_TURNING_OFF = R.drawable.wifi2;

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		Log.i(TAG, "************widget on update onoff2*************");
		initWifiSwitcher(context, appWidgetManager);
	}

	private void initWifiSwitcher(Context context,
			AppWidgetManager appWidgetManager) {
		Log.i(TAG, "initWifiSwitcher");
		WifiManager wifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);

		if (wifiManager.isWifiEnabled()) {
			updateWifiSwitcher(context, RES_ID_TURN_ON, appWidgetManager);
			Log.i(TAG, "isWifiEnabled");
		} else {
			updateWifiSwitcher(context, RES_ID_TURN_OFF, appWidgetManager);
			Log.i(TAG, "isWifiEnabledFalse");
		}
	}

	private void updateWifiSwitcher(Context context, int resId,
			AppWidgetManager appWidgetManager) {

		Log.i(TAG, "updateWifiSwitcher");
		RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.wifionoffwidget);
		Intent intent = new Intent(context, WifiSettingServiceOnOff.class);
		intent.setAction(Intent.ACTION_VIEW);
		PendingIntent pendingIntent = PendingIntent.getService(context, 0,
				intent, 0);
		views.setOnClickPendingIntent(R.id.wifi_switch_button_onoff,
				pendingIntent);
		Log.i(TAG, "update view onoff2");
		views.setImageViewResource(R.id.wifi_status_onoff, resId);
		appWidgetManager.updateAppWidget(new ComponentName(context,
				WifiOnOff2Widget.class), views);
	}

	@Override
	public void onReceive(Context context, Intent intent) {

		WifiManager wifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);

		AppWidgetManager appWidgetManager = AppWidgetManager
				.getInstance(context);
		// SCAN_RESULTS_AVAILABLE_ACTION
		if (intent.getAction()
				.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
			if (isWifiConnected) {
				return;
			}
		}

		// SUPPLICANT_STATE_CHANGED_ACTION
		else if (intent.getAction().equals(
				WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
			isWifiConnected = intent.getBooleanExtra(
					WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
			// supplicantState = (SupplicantState) intent
			// .getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
			// updateWifiButton("Obtaining IP Address...");

		}

		// SUPPLICANT_CONNECTION_CHANGE_ACTION
		else if (intent.getAction().equals(
				WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
			isWifiConnected = intent.getBooleanExtra(
					WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);

		}

		// RSSI_CHANGED_ACTION (signal strength)
		else if (intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)) {

		}

		// NETWORK_IDS_CHANGED_ACTION
		else if (intent.getAction().equals(
				WifiManager.NETWORK_IDS_CHANGED_ACTION)) {
		}

		// NETWORK_STATE_CHANGED_ACTION
		else if (intent.getAction().equals(
				WifiManager.NETWORK_STATE_CHANGED_ACTION)) {

			WifiInfo wifiInfo = wifiManager.getConnectionInfo();
			if (wifiInfo.getIpAddress() > 0) {
				updateWifiSwitcher(context, RES_ID_TURN_ON, appWidgetManager);
			}

			int supperError = intent.getExtras().getInt(
					WifiManager.EXTRA_SUPPLICANT_ERROR);
			if (supperError == WifiManager.ERROR_AUTHENTICATING) {

			}
		}

		// WIFI_STATE_CHANGED_ACTION
		else if (intent.getAction().equals(
				WifiManager.WIFI_STATE_CHANGED_ACTION)) {
			currentWifiState = intent.getExtras().getInt(
					WifiManager.EXTRA_WIFI_STATE);

			if (currentWifiState == WifiManager.WIFI_STATE_ENABLING) {
				updateWifiSwitcher(context, RES_ID_TURNING_ON, appWidgetManager);
				// updateWifiSwitcher(context, RES_ID_TURN_ON,appWidgetManager);
			} else if (currentWifiState == WifiManager.WIFI_STATE_ENABLED) {
				updateWifiSwitcher(context, RES_ID_TURN_ON, appWidgetManager);
			} else if (currentWifiState == WifiManager.WIFI_STATE_DISABLED) {
				updateWifiSwitcher(context, RES_ID_TURN_OFF, appWidgetManager);
			} else if (currentWifiState == WifiManager.WIFI_STATE_DISABLING) {
				updateWifiSwitcher(context, RES_ID_TURNING_OFF,
						appWidgetManager);
				// updateWifiSwitcher(context,
				// RES_ID_TURN_OFF,appWidgetManager);
			} else if (currentWifiState == WifiManager.WIFI_STATE_UNKNOWN) {

			}
		}
		super.onReceive(context, intent);
	}
}
